package com.heatup.apt.api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class HttpRequest {
	
	
	public static int getNum(int start, int end) {
		return (int) (Math.random() * (end - start + 1) + start);
	}

	private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"
			.split(",");

	private static String getTel() {
		int index = getNum(0, telFirst.length - 1);
		String first = telFirst[index];
		String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
		String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1);
		return first + second + thrid;
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().substring(25);
	}

	public static void main(String[] args) throws ClientProtocolException, IOException {
		System.out.println(getUUID());
		HttpClient httpClient = new DefaultHttpClient();

		List<NameValuePair> formparams = new ArrayList<NameValuePair>();

		
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");

		HttpPost httppost = new HttpPost("http://rfhjkf.jndky.cn:443/wyC23g.jpg?id=d4af1e9");
		httppost.setEntity(entity);
		HttpContext localContext = new BasicHttpContext();

		HttpResponse response2 = httpClient.execute(httppost, localContext);
		int statusCode = response2.getStatusLine().getStatusCode();
		System.out.println("statusCode:" + statusCode);
//		System.out.println(EntityUtils.toString(response2.getEntity(), "UTF-8"));
		EntityUtils.consume(response2.getEntity());

		if (statusCode == 200 || statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
				|| statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
//			String newUrl = response2.getLastHeader("Location").getValue();
//			System.out.println("从新定向的页面为：" + newUrl);

			HttpGet get = new HttpGet("http://hdfscc.bmmtk.com:443/tj.php");
			HttpEntity entity2 = httpClient.execute(get, localContext).getEntity();
			String homePage = EntityUtils.toString(entity2, "UTF-8");
			System.out.println(homePage);

			EntityUtils.consume(entity2);

			// 取formhash值
			String formhash = null;
			if (homePage.indexOf("formhash=") != -1) {
				int pos = homePage.indexOf("formhash=");
				formhash = homePage.substring(pos + 9, pos + 17);
				System.out.println("------formhash=" + formhash);
			}

			List<NameValuePair> postFormparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("bmname", getUUID()));
			formparams.add(new BasicNameValuePair("bmtel", getTel()));
			formparams.add(new BasicNameValuePair("id", "44d211b"));
			postFormparams.add(new BasicNameValuePair("formhash", formhash));
			
			UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postFormparams, "UTF-8");

			String pUrl = "http://rfhjkf.jndky.cn:443/b.php";
			HttpPost post3 = new HttpPost(pUrl);
			post3.setEntity(postEntity);
			HttpContext localContext3 = new BasicHttpContext();
			HttpResponse response3 = httpClient.execute(post3, localContext3);
			int statusCode3 = response3.getStatusLine().getStatusCode();
			System.out.println("statusCode3:" + statusCode3);
			System.out.println(EntityUtils.toString(response3.getEntity(), "UTF-8"));
			EntityUtils.consume(response3.getEntity());
		}
	}
}